import { craeteEffect, effectTypes } from './helper'
import { proc } from '../runSaga'

/**
 * all 该函数传入一个数组，数组中放入生成器，saga 会等待所有的生成器全部完成后才会进一步处理
 * @export
 * @param {array} generators 生成器函数 
 */
export function all(generators = []) {
    return craeteEffect(effectTypes.ALL, { generators })
}

/**
 * 处理 all effect 
 * @export
 * @param {*} env
 * @param {*} effect
 * @param {*} next
 */
export function runAllEffect(env, effect, next) {
    const { payload: { generators } } = effect
    const tasks = generators.map(genr => proc(env, genr()))
    // 等到全部的task全部完成之后，调用next
    Promise.all(tasks.map(t => t.toPromise())).then(t => next(), err => next(null, err))
}